<!doctype html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>管理控制台 - 智能水表抄表系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="/static/styles/main.css">
</head>

<body>
    <header class="header">
        <h1><i class="fas fa-tint"></i> 智能水表抄表系统 - 管理控制台</h1>
    </header>

    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-light bg-light shadow-sm">
        <div class="container">
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
                aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="/"><i class="fas fa-home"></i> 首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/history"><i class="fas fa-history"></i> 历史记录</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="/admin"><i class="fas fa-user-shield"></i> 管理控制台</a>
                    </li>
                </ul>
                <div class="navbar-nav">
                    <span class="nav-item nav-link">
                        <i class="fas fa-user"></i> {{ user.username }}
                    </span>
                    <a class="nav-link" href="/logout"><i class="fas fa-sign-out-alt"></i> 退出登录</a>
                </div>
            </div>
        </div>
    </nav>

    <div class="container py-4">
        <div class="row mb-4">
            <div class="col-md-12">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h2><i class="fas fa-user-shield"></i> 管理员控制台</h2>
                        <p>欢迎，{{ user.username }}！</p>
                    </div>
                    <div class="d-flex gap-2">
                        <a href="/admin/audit" class="btn btn-info">
                            <i class="fas fa-shield-alt"></i> 系统安全审计
                        </a>
                        <a href="/" class="btn btn-custom-secondary">
                            <i class="fas fa-home"></i> 返回首页
                        </a>
                        <a href="/logout" class="btn btn-outline-danger">
                            <i class="fas fa-sign-out-alt"></i> 退出登录
                        </a>
                    </div>
                </div>
            </div>
        </div>

        {% if error %}
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            {{ error }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        {% endif %}

        {% if success %}
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            {{ success }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        {% endif %}

        <div class="row mb-4">
            <div class="col-md-3 col-sm-6 mb-4">
                <div class="card bg-primary text-white">
                    <div class="card-body text-center">
                        <i class="fas fa-users fa-3x mb-3"></i>
                        <h3>{{ users|length }}</h3>
                        <p>用户数</p>
                        <a href="#userSection" class="stretched-link"></a>
                    </div>
                </div>
            </div>
            <div class="col-md-3 col-sm-6 mb-4">
                <div class="card bg-info text-white">
                    <div class="card-body text-center">
                        <i class="fas fa-tachometer-alt fa-3x mb-3"></i>
                        <h3>{{ readings|length }}</h3>
                        <p>抄表记录</p>
                        <a href="#readingSection" class="stretched-link"></a>
                    </div>
                </div>
            </div>
            <div class="col-md-3 col-sm-6 mb-4">
                <div class="card bg-warning text-white">
                    <div class="card-body text-center">
                        <i class="fas fa-tasks fa-3x mb-3"></i>
                        <h3 id="pendingBillsCount">-</h3>
                        <p>待审核账单</p>
                        <a href="/admin/pending_bills" class="stretched-link"></a>
                    </div>
                </div>
            </div>
            <div class="col-md-3 col-sm-6 mb-4">
                <div class="card bg-success text-white">
                    <div class="card-body text-center">
                        <i class="fas fa-chart-line fa-3x mb-3"></i>
                        <h3>{{ readings|length }}</h3>
                        <p>系统活跃度</p>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-6 mb-4">
                <div class="card">
                    <div class="card-header">
                        <i class="fas fa-users"></i> 用户管理
                    </div>
                    <div class="card-body">
                        <div class="table-container">
                            <table class="custom-table">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>用户名</th>
                                        <th>角色</th>
                                        <th>注册时间</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for user_item in users %}
                                    <tr>
                                        <td>{{ user_item.id }}</td>
                                        <td>{{ user_item.username }}</td>
                                        <td>
                                            {% if user_item.role == 'admin' %}
                                            <span class="badge bg-danger">管理员</span>
                                            {% else %}
                                            <span class="badge bg-primary">用户</span>
                                            {% endif %}
                                        </td>
                                        <td>{{ user_item.create_time }}</td>
                                        <td>
                                            <a href="/admin/edit_user/{{ user_item.id }}" class="btn btn-sm btn-primary">
                                                <i class="fas fa-edit"></i> 编辑
                                            </a>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <i class="fas fa-tint"></i> 最近抄表记录
                    </div>
                    <div class="card-body">
                        <div class="table-container">
                            <table class="custom-table">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>水表编码</th>
                                        <th>读数</th>
                                        <th>用户</th>
                                        <th>时间</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for reading in readings[:10] %}
                                    <tr>
                                        <td>{{ reading.id }}</td>
                                        <td>{{ reading.meter_code }}</td>
                                        <td>{{ reading.reading_value }}</td>
                                        <td>
                                            {% for user_item in users %}
                                                {% if user_item.id == reading.user_id %}
                                                    {{ user_item.username }}
                                                {% endif %}
                                            {% endfor %}
                                        </td>
                                        <td>{{ reading.timestamp }}</td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <footer class="footer">
        <div class="container">
            <p>&copy; 2023 智能水表抄表系统 | 提供水表读数和编码识别</p>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
        crossorigin="anonymous"></script>
        
    <script>
        // 获取待审核账单数量
        fetch('/admin/pending_bills_count')
            .then(response => response.json())
            .then(data => {
                document.getElementById('pendingBillsCount').textContent = data.count;
            })
            .catch(error => {
                console.error('获取待审核账单数量失败:', error);
                document.getElementById('pendingBillsCount').textContent = '?';
            });
    </script>
</body>

</html> 